﻿using System.Windows.Controls;

namespace CYPlayer.ViewModel
{
    using System;
    using System.IO;
    using System.Windows.Media.Imaging;
    using DataClasses;
    using global::ViewModel;

    public class SongVM : ViewModelBase
    {
        #region Private Members

        private Song _song;
        private TagLib.File _file;
        private BitmapImage _cover;
        
        #endregion

        #region Properties

        public Song Song
        {
            get { return _song; }
            set { _song = value; }
        }

        [CLSCompliant(false)]
        public TagLib.File Mp3File
        {
            get
            {
                if (_file == null && File.Exists(Song.Path))
                {
                    _file = TagLib.File.Create(Song.Path);
                }

                return _file;
            }

            set
            {
                _file = value;
                RaisePropertyChanged("Mp3File");
            }
        }

        public string Title
        {
            get
            {
                return Song.Title;
            }

            set
            {
                Song.Title = value;
                RaisePropertyChanged("Title");
            }
        }

        public string ArtistName
        {
            get
            {
                return Song.ArtistName;
            }

            set
            {
                Song.ArtistName = value;
                RaisePropertyChanged("ArtistName");
            }
        }

        public string GenreName
        {
            get
            {
                return Song.GenreName;
            }

            set
            {
                Song.GenreName = value;
                RaisePropertyChanged("GenreName");
            }
        }
        
        public TimeSpan Duration
        {
            get { return Song.ConvertTotalSeconds(Song.Duration); }
        }

        public BitmapImage Cover
        {
            get
            {
                if (_cover == null)
                {
                    _cover = new BitmapImage();
                    _cover.BeginInit();
                    if (Mp3File.Tag.Pictures.Length > 0)
                    {
                        var bin = Mp3File.Tag.Pictures[0].Data.Data;
                        var stream = new MemoryStream(bin);
                        stream.Seek(0, SeekOrigin.Begin);
                        
                        _cover.StreamSource = stream;
                        
                    }
                    else
                    {
                        _cover.UriSource = new Uri("..\\..\\Unknown.png", UriKind.Relative);
                        _cover.CacheOption = BitmapCacheOption.OnLoad;
                    }
                    _cover.EndInit();
                }
                return _cover;
            }

            set
            {
                _cover = value;
                RaisePropertyChanged("Cover");
            }
        }
        #endregion    
    }
}
